#include "../apue.h"
#include <sys/wait.h>
#include <unistd.h>

#define PS_FMT "pid,ppid,pgid,comm,stat"

int main(void)
{
    pid_t pid;
    pid_t ppid = getppid();

    TELL_WAIT();

    printf("%-14s:%ld, ppid: %ld pgid:%ld\n",
            "current pid", (long)getpid(), (long)ppid, (long)getpgid(0));

    if ((pid = fork()) < 0)
        err_sys("fork error");
    else if (pid == 0)
    {
       setpgid(0, 0);
//       if (setsid() < 0)
//           err_sys("setsid error");

        printf("session id: %ld\n", (long)getsid(0));

        printf("%-14s:%ld, ppid:%ld pgid:%ld\n",
                "child pid", (long)getpid(), (long)getppid(), (long)getpgid(0));
        TELL_PARENT(getppid());

        printf("\npid:%ld and command ps\n:", (long)getpid());
        execl("/bin/ps", "ps_guopong", "-o", PS_FMT, NULL);
    }

    WAIT_CHILD();

    if (waitpid(pid, NULL, 0) < 0)
        err_sys("waitpid error");

    exit(0);
}
